﻿/*
 * Created by SharpDevelop.
 * User: CeCi
 * Date: 02/04/2014
 * Time: 06:08 p.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using Gestión_UTN.Entities;

namespace Gestión_UTN.daimpl
{
    /// <summary>
    /// Description of materiaProfesordaimpl.
    /// </summary>
    public class materiaProfesordaimpl : Gestión_UTN.da.materiaProfesorda
    {
        public materiaProfesordaimpl()
        {
        }

        public bool SaveMateriaProfesor(int mat_id, int pro_dni)
        {
            bool ingresada = false;

            OleDbConnection conex = null;
            OleDbTransaction trans = null;

            try
            {
                conex = util.Conexion.conectarAccess();
                trans = null;

                if (null != conex)
                {

                    string sql = "INSERT INTO mxp_materiasProfesores(mat_id, pro_dni)" +
                                " VALUES(@mat_id, @pro_dni)";

                    conex.Open();
                    trans = conex.BeginTransaction();

                    OleDbCommand insertar = new OleDbCommand(sql, conex, trans);

                    insertar.Parameters.AddWithValue("@mat_id", mat_id);
                    insertar.Parameters.AddWithValue("@pro_dni", pro_dni);

                    insertar.ExecuteNonQuery();

                    trans.Commit();

                    ingresada = true;

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                ingresada = false;
                trans.Rollback();
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return ingresada;
        }

        public bool DeleteMateriaProfesor(int mat_id, int pro_dni)
        {
            bool eliminada = false;

            OleDbConnection conex = null;
            OleDbTransaction trans = null;

            try
            {
                conex = util.Conexion.conectarAccess();
                trans = null;

                if (null != conex)
                {

                    string sql = "DELETE FROM mxp_materiasProfesores " +
                        "WHERE mat_id = @mat_id AND pro_dni = @pro_dni";

                    conex.Open();

                    trans = conex.BeginTransaction();

                    OleDbCommand eliminar = new OleDbCommand(sql, conex, trans);

                    eliminar.Parameters.AddWithValue("@mat_id", mat_id);
                    eliminar.Parameters.AddWithValue("@pro_dni", pro_dni);

                    eliminar.ExecuteNonQuery();

                    trans.Commit();

                    eliminada = true;

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                eliminada = false;
                trans.Rollback();
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return eliminada;
        }

        public bool ModifyMateriaProfesor(int mat_id_anterior, int pro_dni_anterior, int mat_id, int pro_dni)
        {
            bool modificada = false;

            OleDbConnection conex = null;
            OleDbTransaction trans = null;

            try
            {
                conex = util.Conexion.conectarAccess();
                trans = null;

                if (null != conex)
                {

                    string sql = "UPDATE mxp_materiasProfesores SET mat_id = @mat_id, pro_dni = @pro_dni " +
                                    "WHERE mat_id = @mat_id_anterior AND pro_dni = @pro_dni_anterior";

                    conex.Open();
                    trans = conex.BeginTransaction();

                    OleDbCommand modificar = new OleDbCommand(sql, conex, trans);

                    modificar.Parameters.AddWithValue("@mat_id", mat_id);
                    modificar.Parameters.AddWithValue("@pro_dni", pro_dni);
                    modificar.Parameters.AddWithValue("@mat_id_anterior", mat_id_anterior);
                    modificar.Parameters.AddWithValue("@pro_dni_anterior", pro_dni_anterior);

                    modificar.ExecuteNonQuery();

                    trans.Commit();

                    modificada = true;

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                modificada = false;
                trans.Rollback();
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return modificada;
        }

        public System.Data.DataTable FindAllMateriaProfesor()
        {
            DataTable tablaMatPro = null;

            OleDbConnection conex = null;

            try
            {
                conex = util.Conexion.conectarAccess();

                tablaMatPro = new DataTable();

                if (null != conex)
                {

                    string sql = "SELECT mat_nombre, pro_nombre, pro_apellido, car_nombre" +
                                    " FROM mxp_materiasProfesores mxp, mat_materias mat, pro_profesores pro, car_carreras car" +
                                    " WHERE mxp.mat_id = mat.mat_id AND mxp.pro_dni = pro.pro_dni AND mat.car_id = car.car_id";

                    OleDbDataAdapter adaptador = new OleDbDataAdapter(sql, conex);

                    conex.Open();

                    adaptador.Fill(tablaMatPro);

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                tablaMatPro = null;
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return tablaMatPro;
        }

        public System.Collections.Generic.List<Gestión_UTN.Entities.MateriaProfesor> FindMateriaProfesorByDni(int pro_dni)
        {
            List<MateriaProfesor> matPro = null;

            OleDbConnection conex = null;

            try
            {
                conex = util.Conexion.conectarAccess();

                if (null != conex)
                {

                    string sql = "SELECT mat_id, pro_dni" +
                                    " FROM mxp_materiasProfesores mxp WHERE pro_dni = @pro_dni";

                    OleDbCommand consulta = new OleDbCommand(sql, conex);

                    conex.Open();

                    consulta.Parameters.AddWithValue("@pro_dni", pro_dni);

                    OleDbDataReader lector = consulta.ExecuteReader();

                    while (lector.Read())
                    {
                        int mat_id = Convert.ToInt32(lector[0].ToString());
                        int dni = Convert.ToInt32(lector[1].ToString());

                        MateriaProfesor mp = new MateriaProfesor(mat_id, dni);

                        matPro.Add(mp);

                        mp = null;
                    }

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                matPro = null;
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return matPro;
        }

        public System.Collections.Generic.List<Gestión_UTN.Entities.MateriaProfesor> FindMateriaProfesorByMateria(int mat_id)
        {
            List<MateriaProfesor> matPro = null;

            OleDbConnection conex = null;

            try
            {
                conex = util.Conexion.conectarAccess();

                if (null != conex)
                {

                    string sql = "SELECT mat_id, pro_dni" +
                                    " FROM mxp_materiasProfesores mxp WHERE mat_id = @mat_id";

                    OleDbCommand consulta = new OleDbCommand(sql, conex);

                    conex.Open();

                    consulta.Parameters.AddWithValue("@mat_id", mat_id);

                    OleDbDataReader lector = consulta.ExecuteReader();

                    while (lector.Read())
                    {
                        int mat = Convert.ToInt32(lector[0].ToString());
                        int dni = Convert.ToInt32(lector[1].ToString());

                        MateriaProfesor mp = new MateriaProfesor(mat, dni);

                        matPro.Add(mp);

                        mp = null;
                    }

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                matPro = null;
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return matPro;
        }
    }
}
